﻿using System;
using System.Linq.Expressions;
using System.Reflection;

namespace Lucene.Linq.Expressions
{
    internal static class LambdaExpressionHelper
    {
        public static PropertyInfo FindPropertyInfoAccess(this LambdaExpression lambda)
        {
            if (lambda.Body is MemberExpression)
            {
                var memberExpression = lambda.Body as MemberExpression;
                if ((memberExpression.Member is PropertyInfo) == false)
                {
                    throw new ArgumentException("Order by lambda expression should reference property");
                }

                return memberExpression.Member as PropertyInfo;
            }
            throw new ArgumentException("Invalid order by expression. Body of expression should access a member");
        }
    }
}